import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:caose/caose.dart' show dip,sp;

import 'Button.dart';

const _kDefaultNavBarBorder = Border(
  bottom: BorderSide(
    color: Color(0x4C000000),
    width: 0.0, // One physical pixel.
    style: BorderStyle.solid,
  ),
);

class BarWidget extends StatelessWidget {
  const BarWidget(
      {Key key,
        this.child,
        this.title,
        this.backgroundColor = const Color(0xFFF5F6F8),
        this.leading,
        this.middle,
        this.trailing,
        this.right,
        this.rightClick,
        this.drawer,
        this.leftClick,
        this.backColor,
        this.rightColor,
        this.context,
        this.canResize = true,
        this.border = _kDefaultNavBarBorder,
        this.showBack = true,
        this.routes = const <String, WidgetBuilder>{},
        this.topBarBgColor = Colors.white, this.rightStyle})
      : assert(child != null),
        super(key: key);
  final Widget child;
  //topbar
  final Widget leading;
  final Widget middle;
  final Widget trailing;
  final Color topBarBgColor;
  final String title;
  final String right;
  final TextStyle rightStyle;
  final Color rightColor;
  final Widget drawer;
  final VoidCallback rightClick;
  final VoidCallback leftClick;
  final Color backColor;
  final bool showBack;
  final bool canResize;
  final Border border;
  final BuildContext context;
  final Color backgroundColor;
  final Map<String, WidgetBuilder> routes;

  Widget _getMiddle(BuildContext context) {
    if (middle != null) {
      return middle;
    }
    if (title == null) {
      return null;
    }
    return Text(
      title,
      style: TextStyle(fontSize: sp(18),fontWeight: FontWeight.w500),
    );
  }

  Widget _getLeading(BuildContext context) {
    if (leading != null) {
      return leading;
    }
    return Button(
      onPressed: leftClick ??
              () {
            Navigator.maybePop(context);
          },
      padding: EdgeInsets.all(0),
      child: Icon(
        CupertinoIcons.back,
        color: backColor ?? CupertinoColors.black,
        size: 24,
      ),
      pressedOpacity: 0.6,
      minSize: 25,
    );
  }

  Widget _getTrailing(BuildContext context) {
    if (trailing != null) {
      return trailing;
    }
    if (rightClick != null && right != null) {
      return CupertinoButton(
        onPressed: rightClick,
        //pressedOpacity=0.6,
        padding: EdgeInsets.all(0),
        child: Text(
          right,
          style: rightStyle??TextStyle(
              fontSize: sp(15),
              color: rightColor ?? CupertinoColors.activeBlue),
        ),
        pressedOpacity: 0.6,
        minSize: 25,
      );
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        CupertinoPageScaffold(
          backgroundColor: backgroundColor,
          resizeToAvoidBottomInset: canResize,
          navigationBar: CupertinoNavigationBar(
            border: border,
            automaticallyImplyMiddle: title != null || middle != null,
            automaticallyImplyLeading: leading != null && showBack,
            transitionBetweenRoutes: false,
            previousPageTitle: title,
            leading: showBack ? _getLeading(context) : null,
            trailing: _getTrailing(context),
            middle: _getMiddle(context),
            backgroundColor: topBarBgColor,
          ),
          child: child,
        ),
        Container(
          child: drawer,
        ),
      ],
    );
  }
}

class MBarWidget extends StatelessWidget {
  const MBarWidget(
      {Key key,
        this.child,
        this.title,
        this.backgroundColor = const Color(0xFFF5F6F8),
        this.leading,
        this.middle,
        this.trailing,
        this.right,
        this.rightClick,
        this.drawer,
        this.leftClick,
        this.backColor,
        this.rightColor,
        this.context,
        this.canResize = true,
        this.border = _kDefaultNavBarBorder,
        this.showBack = true,
        this.routes = const <String, WidgetBuilder>{},
        this.topBarBgColor = Colors.white, this.rightStyle})
      : assert(child != null),
        super(key: key);
  final Widget child;

  //topbar
  final Widget leading;
  final Widget middle;
  final Widget trailing;
  final Color topBarBgColor;
  final String title;
  final String right;
  final TextStyle rightStyle;
  final Color rightColor;
  final Widget drawer;
  final VoidCallback rightClick;
  final VoidCallback leftClick;
  final Color backColor;
  final bool showBack;
  final bool canResize;
  final Border border;
  final BuildContext context;

  //

  final Color backgroundColor;
  final Map<String, WidgetBuilder> routes;

  Widget _getMiddle(BuildContext context) {
    if (middle != null) {
      return middle;
    }
    if (title == null) {
      return null;
    }
    return Text(
      title,
      style: TextStyle(fontSize: dip(18),fontWeight: FontWeight.w500),
    );
  }

  Widget _getLeading(BuildContext context) {
    if (leading != null) {
      return leading;
    }
    return Button(
      onPressed: leftClick ??
              () {
            Navigator.maybePop(context);
          },
      padding: EdgeInsets.zero,
      child: Image.asset("images/mine/icon_back_gray.png",height: dip(18),color: backColor ?? null,),
      pressedOpacity: 0.9,
      minSize: dip(25),
    );
  }

  Widget _getTrailing(BuildContext context) {
    if (trailing != null) {
      return trailing;
    }
    if (rightClick != null && right != null) {
      return CupertinoButton(
        onPressed: rightClick,
        padding: EdgeInsets.all(0),
        child: Text(
          right,
          style: rightStyle??TextStyle(
              fontSize: sp(15),
              color: rightColor ?? CupertinoColors.activeBlue),
        ),
        pressedOpacity: 0.9,
        minSize: dip(25),
      );
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Scaffold(
          backgroundColor: backgroundColor,
          resizeToAvoidBottomInset: canResize,
          appBar: CupertinoNavigationBar(
            border: border,
            automaticallyImplyMiddle: title != null || middle != null,
            automaticallyImplyLeading: leading != null && showBack,
            transitionBetweenRoutes: false,
            previousPageTitle: title,
            leading: showBack ? _getLeading(context) : null,
            trailing: _getTrailing(context),
            middle: _getMiddle(context),
            backgroundColor: topBarBgColor,
          ),
          body: child,
          drawer: drawer,
        ),

      ],
    );
  }
}
